package com.xindaoapp.happypet.ui;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.application.HappyPetApplication;
import com.xindaoapp.happypet.protocol.MoccaApi;
import com.xindaoapp.happypet.utils.CommonUtil;

/** @author chenghao */
public abstract class BaseActivity extends UMBaseActivity {
    /** 播放加载时动画 */
    public FrameLayout loading;
    private ImageView imageView;
    private AnimationDrawable animation;
    public View load_fail;

    private boolean isFirstLoading = true;
    protected ImageView rightImageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getContentViewResId() != 0) {
            setContentView(getContentViewResId());
        }
        initConfigs();
        initDefaultViews();
        initViews();
        initEvents();
        loadDatas();
    }

    /**
     * 初始化一些配置信息
     */
    protected void initConfigs() {}

    /**
     * 获取窗口内容布局
     * 
     * @return
     */
    protected abstract int getContentViewResId();

    protected void initViews() {}

    protected void initEvents() {}

    /**
     * 初始化一些公共的view信息
     */
    private void initDefaultViews() {
        if (null == findViewById(R.id.top_bar)) {
            return;
        }
        ImageView leftImageView = (ImageView) findViewById(R.id.top_bar_left_imageview);
        TextView leftTextView = (TextView) findViewById(R.id.top_bar_left_textview);
        if (getTopBarLeftImageViewRes() != 0) {
            leftImageView.setImageResource(getTopBarLeftImageViewRes());
            leftImageView.setOnClickListener(getLeftViewClickListener());
        } else if (getTopBarLeftTextViewRes() != 0) {
            leftTextView.setText(getTopBarLeftTextViewRes());
            leftTextView.setOnClickListener(getLeftViewClickListener());
        }
        rightImageView = (ImageView) findViewById(R.id.top_bar_right_imageview);
        TextView rightTextView = (TextView) findViewById(R.id.top_bar_right_textview);
        if (getTopBarRightImageViewRes() != 0) {
            rightImageView.setImageResource(getTopBarRightImageViewRes());
            rightImageView.setOnClickListener(getRightViewClickListener());
        } else if (getTopBarRightTextViewRes() != 0) {
            rightTextView.setText(getTopBarRightTextViewRes());
            rightTextView.setOnClickListener(getRightViewClickListener());
        }
        // else {
        // rightImageView.setVisibility(View.GONE);
        // rightTextView.setVisibility(View.GONE);
        // }
        if (getTopBarTitleRes() != 0) {
            ((TextView) findViewById(R.id.top_bar_title)).setText(getTopBarTitleRes());
        } else if (!TextUtils.isEmpty(getTopBarTitleStrRes())) {
            ((TextView) findViewById(R.id.top_bar_title)).setText(getTopBarTitleStrRes());
        }
    }

    /**
     * 加载数据
     */
    protected void onLoadDatas() {}

    private void loadDatas() {
        loading = (FrameLayout) findViewById(R.id.loading);
        load_fail = findViewById(R.id.ll_load_fail);

        if (load_fail != null && loading != null) {
            // 重新加载
            load_fail.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    if (imageView != null) {
                        load_fail.setVisibility(View.GONE);
                        loading.setVisibility(View.VISIBLE);
                        imageView.setBackgroundResource(R.anim.alertdialog_animation);
                        animation = (AnimationDrawable) imageView.getBackground();
                        animation.setOneShot(false);
                        // animation.start();
                        startAnimation();
                    }
                    loadDatas();
                }
            });
            imageView = (ImageView) findViewById(R.id.imageView);
            if (imageView != null) {
                imageView.setBackgroundResource(R.anim.alertdialog_animation);
                animation = (AnimationDrawable) imageView.getBackground();
                animation.setOneShot(false);
                // animation.start();
                startAnimation();
            }
            loading.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            });
        }

        if (CommonUtil.isNetworkAvailable(getApplicationContext()) == 0) {
            onDataArrived(false);
        }

        onLoadDatas();
    }


    public void startAnimation() {
        if (loading.getVisibility() == View.GONE) {
            loading.setVisibility(View.VISIBLE);
        }
        if (load_fail.getVisibility() == View.VISIBLE) {
            load_fail.setVisibility(View.GONE);
        }
        if (animation != null && null != imageView) {
            animation.start();
        }
    }

    /**
     * 数据到达状态
     * 
     * @param isArrived true 数据到达，false 数据未到达
     */
    protected final void onDataArrived(boolean isArrived) {
        if (isArrived) {
            isFirstLoading = false;
            if (animation != null && null != imageView) {
                animation.stop();
                if (load_fail.getVisibility() == View.VISIBLE) {
                    load_fail.setVisibility(View.GONE);
                }
                loading.setVisibility(View.GONE);
            }
        } else {
            if (!isFirstLoading) {
                Toast.makeText(getApplicationContext(), R.string.net_error, Toast.LENGTH_SHORT)
                        .show();
                if (animation != null && null != imageView) {
                    animation.stop();
                    if (load_fail.getVisibility() == View.VISIBLE) {
                        load_fail.setVisibility(View.GONE);
                    }
                    loading.setVisibility(View.GONE);
                }
            } else if (animation != null && null != imageView) {
                animation.stop();
                if (loading.getVisibility() == View.GONE) {
                    loading.setVisibility(View.VISIBLE);
                }
                if (load_fail.getVisibility() == View.GONE) {
                    load_fail.setVisibility(View.VISIBLE);
                }
            }
        }
    }

    /**
     * 请求成功，但是数据为空，页面显示暂无数据
     */
    protected final void onDataArrivedEmpty() {
        View view = findViewById(R.id.no_data);
        if (null != loading && null != view) {
            loading.setVisibility(View.VISIBLE);
            view.setVisibility(View.VISIBLE);
            view.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub

                }
            });
        }
    }

    protected OnClickListener getLeftViewClickListener() {
        return null;
    }

    protected OnClickListener getRightViewClickListener() {
        return null;
    }

    /**
     * 获取顶部导航的左侧图片<br/>
     * 
     * 默认返回图片
     * 
     * @return
     */
    protected int getTopBarLeftImageViewRes() {
        return 0;
    }

    /**
     * 获取顶部导航的左侧文字<br/>
     * 
     * @return
     */
    protected int getTopBarLeftTextViewRes() {
        return 0;
    }

    protected int getTopBarRightImageViewRes() {
        return 0;
    }

    protected int getTopBarRightTextViewRes() {
        return 0;
    }

    protected final View getTopBarRightTextView() {
        return findViewById(R.id.top_bar_right_textview);
    }

    protected final View getTopBarRightImageView() {
        return findViewById(R.id.top_bar_right_imageview);
    }

    protected int getTopBarTitleRes() {
        return 0;
    }

    protected String getTopBarTitleStrRes() {
        return null;
    }

    protected final void setTopBarTitle(int resId) {
        if (null == findViewById(R.id.top_bar)) {
            return;
        }
        ((TextView) findViewById(R.id.top_bar_title)).setText(resId);
    }

    protected final void setTopBarImage(int resId) {
        if (null == findViewById(R.id.top_bar_center_image)) {
            return;
        }
        ((ImageView) findViewById(R.id.top_bar_center_image)).setVisibility(View.VISIBLE);
        ((ImageView) findViewById(R.id.top_bar_center_image)).setImageResource(resId);
    }


    protected final void setTopBarTitle(String res) {
        if (null == findViewById(R.id.top_bar)) {
            return;
        }
        ((TextView) findViewById(R.id.top_bar_title)).setText(res);
    }

    public final void setTopCenterImage(int res) {
        if (null == findViewById(R.id.top_bar_center_image)) {
            return;
        }
        ((ImageView) findViewById(R.id.top_bar_center_image)).setVisibility(View.VISIBLE);
        ((ImageView) findViewById(R.id.top_bar_center_image)).setBackgroundResource(res);;
    }

    protected final MoccaApi getMoccaApi() {
        return HappyPetApplication.get().getMoccaApi();
    }

    /** 隐藏键盘 */
    protected final void hideKeyBoard() {
        InputMethodManager inputManager =
                (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        inputManager.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0);
    }

    protected final void showKeyBoard() {
        InputMethodManager inputManager =
                (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        inputManager.showSoftInputFromInputMethod(getWindow().getDecorView().getWindowToken(), 1);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ImageLoader.getInstance().clearMemoryCache();
    }

    protected void showToast(String str) {
        Toast.makeText(getBaseContext(), str, Toast.LENGTH_SHORT).show();
    }

}
